<?php
namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;
require_once ('Common.php');

/**
 * JS-SDK组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Script extends Common
{
    const GET_TICKET_URL = '/ticket/getticket?';
    private $jsapi_ticket='';
	function __construct($appid,$secret,$token)
	{
        parent::__construct($appid,$secret,$token);
	}

	function __destruct()
	{
	}
	
    /**
     * 获取JSAPI授权TICKET
     * @param string $access_token 获取 jsapi_ticket 指定 access_token
     * @return bool|string
     */
	public function getJsTicket()
	{
        if (!$this->access_token && !$this->getAccessToken()) {
            return false;
        }
        $result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$this->access_token}" . '&type=jsapi');
        $result = $this->parsing($result);
        return $result;
        
        
        
	}
    /**
     * 获取JsApi使用签名
     * @param string $url 网页的URL，自动处理#及其后面部分
     * @param int $timestamp 当前时间戳 (为空则自动生成)
     * @param string $noncestr 随机串 (为空则自动生成)
     * @param string $appid 用于多个appid时使用,可空
     * @param string $access_token 获取 jsapi_ticket 指定 access_token
     * @return array|bool 返回签名字串
     */
    public function getJsSign($url,$debug=false, $jsapi_ticket = '',$timestamp = 0, $noncestr = '',$appid = '') 
    {
        if (!$jsapi_ticket && !$this->jsapi_ticket) 
        {
            $result = $this->getJsTicket();
            if($result['errcode'] ==0 )
            {
                $this->jsapi_ticket = $result['ticket'];
            }
            else
            {
                return false;
            }
        }
        
        $jsapi_ticket = $jsapi_ticket ? $jsapi_ticket : $this->jsapi_ticket;
        $data = array(
            "jsapi_ticket" => $jsapi_ticket,
            "timestamp"    =>  !empty($timestamp) ?  $timestamp : time() ,
            "noncestr"     =>  !empty($noncestr) ? $noncestr : Tools::getRandStr(),
            "url"          => trim($url),
        );
        return array(
            "url"       => $url,
            'debug'     => $debug,
            "appId"     => !empty($appid) ? $appid : $this->appid ,
            "nonceStr"  => $data['noncestr'],
            "timestamp" => $data['timestamp'],
            "signature" => Tools::getSha1Signature($data),
            'jsApiList' => array(
                'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone',
                'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem',
                'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'closeWindow', 'scanQRCode', 'chooseWXPay',
                'translateVoice', 'getNetworkType', 'openLocation', 'getLocation',
                'openProductSpecificView', 'addCard', 'chooseCard', 'openCard',
                'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice',
                'openWXDeviceLib', 'closeWXDeviceLib', 'getWXDeviceInfos', 'sendDataToWXDevice', 'disconnectWXDevice', 'getWXDeviceTicket', 'connectWXDevice',
                'startScanWXDevice', 'stopScanWXDevice', 'onWXDeviceBindStateChange', 'onScanWXDeviceResult', 'onReceiveDataFromWXDevice',
                'onWXDeviceBluetoothStateChange', 'onWXDeviceStateChange'
            )
        );
    }

}
?>